import { Component, OnInit } from '@angular/core';
import { StudentService } from "../student.service";
import { FormBuilder, FormGroup } from "@angular/forms";
import { Student } from "../Student";
import { ClassGrade } from "../ClassGrade";
import { SelectCourse } from "../SelectCourse";
import  { Course } from '../Course';
import {discardPeriodicTasks} from "@angular/core/testing";

@Component({
  selector: 'app-scores',
  templateUrl: './scores.component.html',
  styleUrls: ['./scores.component.css']
})
export class ScoresComponent implements OnInit {
  rClass: Array<ClassGrade> = [];
  selectclass: Array<ClassGrade> = [];

  students: Array<Student> = []; // 所有学生学生信息
  studentsCourses: Array<SelectCourse> = []; // 所有学生选课信息
  courses: Array<Course> = []; // 所有选课课程信息
  studentsScores: Array<ClassGrade> = []; // 学生成绩信息列表

  validateForm: FormGroup;

  constructor(
    private studentService: StudentService,
    private formBuilder: FormBuilder,
  ) {
    this.validateForm = this.formBuilder.group({
      courseid: [''],
      coursename: [''],
      stuid: [''],
      name: [''],
      major: [''],
      class: [''],
      score: [''],
    });
  }

  ngOnInit(): void {
    // this.handleSearch();
    // 获取所有选课信息
    this.getCourses();
  }

  // 提交表单
  submitForm(values) {
    console.log('values', values);
    const coursename = values.coursename.trim();

    this.selectclass = this.rClass.filter(item => {
      const {} = values;

      //过滤课程名
      if (item.coursename.trim().indexOf(coursename) === -1) {
        return false;
      }

      // 过滤专业
      if (values.major && values.major !== item.major) {
        return false;
      }

      // 过滤班级
      if (values.class && values.class !== item.class) {
        return false;
      }

      return true;
    });
  }

  /**
   * 获取所有课程信息
   */
  getCourses() {
    this.studentService.getCourses().subscribe((data: any) => {
      if (!data || data.status !== 1) {
        return false;
      }

      this.courses = data.data;
      const courseid = this.courses.length ? this.courses[0].courseid : '';
      // 设置默认课程为列表中第一项
      this.validateForm.setValue({ courseid });
      // 根据课程 id 获取所有学生选课信息
      this.getCoursesByid({ courseid });
    });
  }

  /**
   * 根据选课 id 获取所有学生选课信息
   * @param {Object} param
   */
  getCoursesByid(param: { [key: string]: string }) {
    this.studentService.getCoursesByCourseid(param).subscribe((data) => {
      console.log('getStudentsScores', data);
      if (!data || data.status !== 1) {
        return false;
      }

      this.studentsCourses = data.data;
      // 获取所有学生信息, 构建学生成绩信息列表
      this.getStudents();
    });
  }

  // 获取所有学生的学生信息
  getStudents(): void {
    this.studentService.getStudents().subscribe(data => {
      if (!data || data.status !== 1) return false;

      this.students = data.data;
      console.log('studentsCourses', this.studentsCourses);
      this.studentsScores = this.studentsCourses.map(course => {
        const student = this.students.find(item => item.stuid === course.stuid);
        const courseData = this.courses.find(item => item.courseid === course.courseid);
        const score: ClassGrade = Object.assign({}, course, {
          name: student.name,
          major: student.major,
          class: student.class,
          coursename: courseData.coursename,
        });
        return score;
      })
    })
  }

  // 查询
  handleSearch(params) {
  }

  resetForm($event: MouseEvent) {
    this.handleSearch(null);
  }
}

